const db = wx.cloud.database()
const data = db.collection('say')
var util = require("../../../../util/util.js")

Page({
  data: {
    userInfo: "",
    fileList: [],
    imgbox: [], //选择图片
    fileIDs: [], //上传云存储后的返回值
  },
  onLoad:function(e){
    var that = this;
    wx.requestSubscribeMessage({
      tmplIds: ['AadcW-r08ZUw7Wl3oZsplWCs9oXLhJAlf25tC-4wuxo'], // 此处可填写多个模板 ID，但低版本微信不兼容只能授权一个
      success(res) {

      }
    })
    that.setData({
      flag:e.flag
    })
    wx.getUserInfo({
      success: function (res) {
        that.setData({
          avatarUrl: res.userInfo.avatarUrl,
          nickName: res.userInfo.nickName,
        })
      }
    })
  },
  // 删除照片 &&
  imgDelete1: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.deindex;
    let imgbox = this.data.imgbox;
    imgbox.splice(index, 1)
    that.setData({
      imgbox: imgbox
    });
  },

  // 选择图片 &&&
  addPic1: function (e) {
    var imgbox = this.data.imgbox;
    console.log(imgbox)
    var that = this;
    var n = 5;
    if (5 > imgbox.length > 0) {
      n = 5 - imgbox.length;
    } else if (imgbox.length == 5) {
      n = 1;
    }
    wx.chooseImage({
      count: 9, // 默认9，设置图片张数
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // console.log(res.tempFilePaths)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths

        if (imgbox.length == 0) {
          imgbox = tempFilePaths
        } else if (9 > imgbox.length) {
          imgbox = imgbox.concat(tempFilePaths);
        }
        that.setData({
          imgbox: imgbox
        });
      }
    })
  },

  //图片
  imgbox: function (e) {
    this.setData({
      imgbox: e.detail.value
    })
  },
  //发布按钮
  formSubmit: function (e) {
    const that = this
    if (e.detail.value.information == '') {
      wx.showModal({
        title: '提示',
        content: '请输入内容',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      })
    } else {
      wx.showLoading({
        title: '提交中...',
      })
      let promiseArr = [];
      for (let i = 0; i < this.data.imgbox.length; i++) {
        promiseArr.push(new Promise((reslove, reject) => {
          let item = this.data.imgbox[i];
          let suffix = /\.\w+$/.exec(item)[0]; //正则表达式返回文件的扩展名
          wx.cloud.uploadFile({
            cloudPath: 'say/' + new Date().getTime() + suffix, // 上传至云端的路径
            filePath: item, // 小程序临时文件路径
            success: res => {
              this.setData({
                fileIDs: this.data.fileIDs.concat(res.fileID)
              });
              
              reslove();
            },
            fail: res => {
              wx.showToast({
                icon:"none",
                title: '上传图片失败',
              })
            }
          })
        }));
      }
      Promise.all(promiseArr).then(res => { //等数组都做完后做then方法
        
        that.fb(e)
      })

    }
  },
  //  添加数据到数据库
  fb: function (event) {
    var that = this
    data.add({
      data: {
        time:util.formatTime(new Date()),
        fileList: that.data.fileIDs,
        nickName: that.data.nickName,
        avatarUrl: that.data.avatarUrl,
        flag:that.data.flag,
        information: event.detail.value.information,
        goodNum:0,
        redu:0,
        isGood:false,
        commentNum:0,
        show: false
      },
      success: function (res) {
        wx.hideLoading({
          success: (res) => {},
        })
        wx.showModal({
          title: '提示',
          content: '审核中，请稍后...',
          success(res){
            if(res.cancel){
              //取消后，啥也不做
            }else if(res.confirm){
              //返回列表
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      },

    })
  }
})